package com.qianfeng.test.fragment;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.qianfeng.test.MapLocationActivity;
import com.qianfeng.test.R;
import com.qianfeng.test.adapter.MainItemAdapter;
import com.qianfeng.test.bean.ShopInfo;
import com.qianfeng.test.constants.UrlConstans;
import com.qianfeng.test.task.MainTask;
import com.qianfeng.test.utils.LocationHelper;

import java.util.ArrayList;

/**
 * Created by Administrator on 15-7-21.
 */
public class SellerFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemClickListener,LocationHelper.BdLocationCallback{
    private TextView seller_head_location;
    private PullToRefreshListView ptrListView;
    private ArrayList<ShopInfo> list = new ArrayList<ShopInfo>();
    private MainItemAdapter mainItemAdapter;
    private ILoadingLayout loadingLayout;
    private boolean isRefresh = false;
    private int page = 1;
    private float lat;
    private float lng;
    private View view;
    private String locationAdd;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_seller,container,false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        new MainTask(mainItemAdapter, list, ptrListView, page).execute(UrlConstans.Url_Seller);
    }

    private void initView(View view) {
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.seller_ptrlistview);

        ListView listView = ptrListView.getRefreshableView();//什么意思

        //设置滑动模式
        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        //设置滑动监听事件
        ptrListView.setOnRefreshListener(this);

        View headview = getActivity().getLayoutInflater().inflate(R.layout.seller_head_layout,null);
        //给ptrListView添加头部
        listView.addHeaderView(headview);

        seller_head_location = (TextView) headview.findViewById(R.id.seller_head_location);

        seller_head_location.setText(locationAdd);


        mainItemAdapter = new MainItemAdapter(list, getActivity());
        ptrListView.setAdapter(mainItemAdapter);//错误
        loadingLayout = ptrListView.getLoadingLayoutProxy();
        ptrListView.setRefreshing(); //

        //设置ptrListView每个item的点击事件
        ptrListView.setOnItemClickListener(this);

        //设置head按钮的定位点击事件
        Button mapButton = (Button) headview.findViewById(R.id.request);
        mapButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MapLocationActivity.class);
                startActivity(intent);
            }
        });
    }




    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), "----->" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        loadingLayout.setLastUpdatedLabel("最后更新时间");
        initData();
        isRefresh = true;
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        page++;
        initData();
    }


    @Override
    public void locationCallback(double lat, double lngt, String address) {
        locationAdd = address;
    }
}
